package com.yunkaweilai.android.view.dialog.shop;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.github.lazylibrary.util.StringUtils;
import com.github.lazylibrary.util.ToastUtils;
import com.yunkaweilai.android.R;
import com.yunkaweilai.android.activity.operation.integral.IntegralTypeActivity;
import com.yunkaweilai.android.activity.shop.ShopTypeActivity;
import com.yunkaweilai.android.constants.Api;
import com.yunkaweilai.android.constants.CommonConstants;
import com.yunkaweilai.android.model.Event;
import com.yunkaweilai.android.net.HttpRequest;
import com.yunkaweilai.android.net.PostProcess;
import com.yunkaweilai.android.utils.Utils;

import org.greenrobot.eventbus.EventBus;

/**
 * 添加大分类
 * Created by Administrator on 2016/12/22 0022.
 */

public class ShopTypeUpdateDialog1 extends Dialog {

    private String id;//分类id
    private Context context;
    private String parent_id;//父级id
    private String goods_category_name;//分类名称
    private String type ;
    private ImageView idImgClose;
    private TextView idTvSave;
    private TextView idTvDel;
    private EditText idEdtTypeName;

    public ShopTypeUpdateDialog1(Context context, int themeResId,String id,String goods_category_name,String parent_id,String type) {
        super(context, themeResId);
        this.context = context;
        this.id = id;
        this.parent_id = parent_id;
        this.goods_category_name = goods_category_name;
        this.type  = type;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_type_update);
        idImgClose = ((ImageView) findViewById(R.id.id_img_close));
        idTvSave = ((TextView) findViewById(R.id.id_tv_save));
        idTvDel = ((TextView) findViewById(R.id.id_tv_del));
        idEdtTypeName = ((EditText) findViewById(R.id.id_edt_type_name));
        idEdtTypeName.setText(goods_category_name);
        idEdtTypeName.setSelection(goods_category_name.length());
        Utils.autoShowKeyboard(idEdtTypeName);//弹出软键盘

        initClick();

    }

    private void initClick() {

        idImgClose.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        idTvSave.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (!StringUtils.isEmpty(idEdtTypeName.getText().toString())){
                    if (type.equals(ShopTypeActivity.PT_SHOP)){
                        upDateType(Api.PT_SHOP_TYPE_SAVE);
                    }else if (type.equals(ShopTypeActivity.TIME_SHOP)){
                        upDateType(Api.TIME_SHOP_TYPE_SAVE);
                    }else if (type.equals(IntegralTypeActivity.INTEGRAL)){
                        upDateType2(Api.APP_SAVE_INTEGRAL_CATEGORY);
                    }
                }
            }
        });
        idTvDel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ShopTypeDelConfirmDialog1 delConfirmDialog = new ShopTypeDelConfirmDialog1(context,id,type,ShopTypeUpdateDialog1.this);
                delConfirmDialog.show();


            }
        });
    }

    //修改分类
    private void upDateType(String url){
        HttpRequest.postUrl(url)
                .addParams("id",id)
                .addParams("parent_id",parent_id)
                .addParams("goods_category_name",idEdtTypeName.getText().toString())
                .addParams("status","1")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode){
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.MAX_TYPE,true));
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.SHOP_LIST_REFRESH,true));
                        }
                        dismiss();
                    }

                    @Override
                    public void onError(Exception e) {
                        ToastUtils.showToast(context,"修改分类失败");
                    }
                });

    }
    //修改分类
    private void upDateType2(String url){
        HttpRequest.postUrl(url)
                .addParams("id",id)
                .addParams("parent_id",parent_id)
                .addParams("gift_category_name",idEdtTypeName.getText().toString())
                .addParams("status","1")
                .execute(new PostProcess.StringCallBack() {
                    @Override
                    public void onResponse(String json) {
                        boolean checkCode = Utils.checkCode(context, json);
                        if (checkCode){
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.MAX_TYPE,true));
                            EventBus.getDefault().post(new Event.TypeEvent(CommonConstants.SHOP_LIST_REFRESH,true));
                        }
                        dismiss();
                    }

                    @Override
                    public void onError(Exception e) {
                        ToastUtils.showToast(context,"修改分类失败");
                    }
                });

    }

}
